package com.huiquan.management.controller;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.management.service.UserManageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserManageController {

	@Autowired
	private UserManageService userManageService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/management/user_manage_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");

		return userManageService.list(keyword);
	}

	@ResponseBody
	@RequestMapping(value = "/management/add_user.do")
	public ReturnData addUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String realName = req.getParameter("realName");
		String userName = req.getParameter("userName");

		return userManageService.addUser(realName, userName);
	}

	@ResponseBody
	@RequestMapping(value = "/management/set_role.do")
	public ReturnData setRole(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String role = req.getParameter("role");

		return userManageService.setRole(id, role);
	}
	@ResponseBody
	@RequestMapping(value = "/management/set_page.do")
	public ReturnData setPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String tabId = req.getParameter("tabId");

		return userManageService.setPage(id, tabId);
	}
	@ResponseBody
	@RequestMapping(value = "/management/hide_user.do")
	public ReturnData hideUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");

		return userManageService.setHide(id, 1);
	}

	@ResponseBody
	@RequestMapping(value = "/management/display_user.do")
	public ReturnData displayUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");

		return userManageService.setHide(id, 0);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/management/user_page_list.htm")
	public ModelAndView userPageList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		User user = (User) req.getSession().getAttribute("user");

		return userManageService.userPage(user);
	}
	@ResponseBody
	@RequestMapping(value = "/management/user_company_id.do")
	public ReturnData setConpanyId(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String companyId = req.getParameter("companyId");

		return userManageService.setConpanyId(StringUtils.isBlank(id) ? 0 : Long.parseLong(id), companyId);
	}
}
